package com.yinhe.music.yhmusic.main.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.type.person.BannerInfo;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.TabAdapter;
import com.yinhe.music.yhmusic.autolisten.AutoListenActivity;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceFragment;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.dialog.SongClassifyDialog;
import com.yinhe.music.yhmusic.instrument.InstrumentActivity;
import com.yinhe.music.yhmusic.model.HomeInfo;
import com.yinhe.music.yhmusic.radio.RadioListActivity;
import com.yinhe.music.yhmusic.singer.SingerActivity;
import com.yinhe.music.yhmusic.songmenu.SongMenuListActivity;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;
import com.yinhe.music.yhmusic.utils.ViewUtils;
import com.yinhe.music.yhmusic.video.VideoActivity;
import com.yinhe.music.yhmusic.widget.ColorFlipPagerTitleView;
import com.youth.banner.Banner;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 音乐馆
 *
 * @author hasee
 * @date 2017/8/15
 */

public class HomePageMusicFragment extends BaseServiceFragment implements
        HomeContract.IHomeView {

    public static final int HOME = 0;

    @BindView(R.id.banner)
    Banner mBanner;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.rl_tabs)
    RelativeLayout rlTabs;
    @BindView(R.id.tabs)
    MagicIndicator indicator;
    @BindView(R.id.screen)
    ImageView ivScreen;

    private String[] mTitles = new String[]{ "推荐", "新歌", "歌单", "视频" };
    private List<String> mDataList = Arrays.asList(mTitles);
    List<Fragment> mFragments;

    HomePresenter mPresenter;

    // 单例
    public static HomePageMusicFragment newInstance() {
        Bundle args = new Bundle();
        HomePageMusicFragment fragment = new HomePageMusicFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new HomePresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_homepage_music;
    }

    @Override
    public void initView() {
        RxBus.get().register(this);

        mBanner.setOffscreenPageLimit(5);
        ViewUtils.setBanner(mBanner);

        setupViewPager();

        mPresenter.loadCacheDataForHome();
        mPresenter.getRecommendBanners();
    }

    private void setupViewPager() {
        mFragments = new ArrayList<>();
        SongFragment     fragment1 = SongFragment.newInstance();
        NewSongFragment  fragment2 = NewSongFragment.newInstance();
        SongMenuFragment fragment3 = SongMenuFragment.newInstance();
        MvFragment       fragment4 = MvFragment.newInstance();
        mFragments.add(fragment1);
        mFragments.add(fragment2);
        mFragments.add(fragment3);
        mFragments.add(fragment4);

        // 第二步：为ViewPager设置适配器
        TabAdapter adapter = new TabAdapter(getChildFragmentManager(), mFragments, mTitles);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                ivScreen.setVisibility(i == 0 || i == 1 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        //  第三步：将ViewPager与MagicIndicator 绑定在一起
        initMagicIndicator();
    }

    @OnClick({R.id.song, R.id.radio, R.id.singer, R.id.video, R.id.auto_listen, R.id.instrument, R.id.screen})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.song:
                UmengEventUtils.clickHome(getActivity(), UmengEventUtils.SONG_MENU);
                startActivity(new Intent(getActivity(), SongMenuListActivity.class));
                break;
            case R.id.singer:
                UmengEventUtils.clickHome(getActivity(), UmengEventUtils.SINGER);
                startActivity(new Intent(getActivity(), SingerActivity.class));
                break;
            case R.id.video:
                UmengEventUtils.clickHome(getActivity(), UmengEventUtils.MV);
                Intent videoIntent = new Intent(getActivity(), VideoActivity.class);
                videoIntent.putExtra("type", IConstants.MORE_VIDEO);
                startActivity(videoIntent);
                break;
            case R.id.radio:
                UmengEventUtils.clickHome(getActivity(), UmengEventUtils.RAIDO);
                startActivity(new Intent(getActivity(), RadioListActivity.class));
                break;
            case R.id.auto_listen:
                UmengEventUtils.clickHome(getActivity(), UmengEventUtils.AUTO_LISTEN);
                startActivity(new Intent(getActivity(), AutoListenActivity.class));
                break;
            case R.id.instrument:
                UmengEventUtils.clickHome(getActivity(), UmengEventUtils.INSTRUMENT);
                startActivity(new Intent(getActivity(), InstrumentActivity.class));
                break;
            case R.id.screen:
                SongClassifyDialog.newInstance().show(getActivity().getSupportFragmentManager(),"songClassify");
                break;
            default:
                break;
        }
    }

    @Override
    public void setRecommend(HomeInfo homeInfo) {
        List<BannerInfo> mDatas = homeInfo.getBanner();
        if (mDatas != null) {
            ViewUtils.updateBanner(mBanner, mDatas, getActivity());
        }
    }

    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorFlipPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(getActivity(), R.color.text_title_11));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(getActivity(), R.color.text_title_11));
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setOnClickListener((v) -> viewPager.setCurrentItem(index, false));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, 20));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(ContextCompat.getColor(getActivity(), R.color.common_red));
                return indicator;
            }
        });
        indicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(indicator, viewPager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(this);
    }

    /**
     * 切换语系
     *
     * @param rxbusNullObject {@link RxbusNullObject}
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Home.CHANGE_LANG_TYPE)
    )
    public void onRxbusChangeLang(RxbusNullObject rxbusNullObject) {
        mPresenter.getRecommendBanners();
    }
}
